package com.d.ui.view.vs;

import com.d.lib.ui.view.vs.VSItem;
import com.d.lib.ui.view.vs.VSView;
import com.d.ui.view.ResourceTable;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * VSView Test
 * Created by D on 2017/4/20.
 */
public class VSViewLayout extends DirectionalLayout implements VSView.OnVSClickListener {
  private Context context;
  private VSView vsv_vs;

  public VSViewLayout(Context context) {
    super(context);
    init(context);
  }

  public VSViewLayout(Context context, AttrSet attrs) {
    super(context, attrs);
    init(context);
  }

  public VSViewLayout(Context context, AttrSet attrs, int defStyleAttr) {
    super(context, attrs, String.valueOf(defStyleAttr));
    init(context);
  }

  private void init(Context context) {
    this.context = context;
    Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_layout_vs,
        this, true);
    vsv_vs = (VSView) view.findComponentById(ResourceTable.Id_vsv_vs);
    initVS();
  }

  private void initVS() {
    VSItem vsA = new VSItem("A", false);
    VSItem vsB = new VSItem("B", false);
    vsv_vs.setCompareA(vsA).setCompareB(vsB).setPercent(-1, false);
    vsv_vs.setOnVSClickListener(this);
  }

  @Override
  public void onItemClick(int index, VSItem item) {
    if (index == 0) {
      vsv_vs.setPercent(0.7f, true);
    } else {
      vsv_vs.setPercent(0.3f, true);
    }
    new ToastDialog(getContext()).setText("Selected: " + item.mainText).setAlignment(LayoutAlignment.BOTTOM).show();
  }
}
